import requests
import ddddocr
import base64
import json

session = requests.Session()

# 登录的地址
url = 'https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx'

# 验证码图片地址
yzm_url = 'https://so.gushiwen.cn/RandCode.ashx'
# 发起网络请求
res = session.get(yzm_url)
with open('yzm.jpg', 'wb') as f:
    f.write(res.content)


# 验证码识别
def base64_api(uname, pwd, img, typeid):
    with open(img, 'rb') as f:
        base64_data = base64.b64encode(f.read())
        b64 = base64_data.decode()
    data = {"username": uname, "password": pwd, "typeid": typeid, "image": b64}
    result = json.loads(requests.post("http://api.ttshitu.com/predict", json=data).text)
    if result['success']:
        return result["data"]["result"]
    else:
        # ！！！！！！！注意：返回 人工不足等 错误情况 请加逻辑处理防止脚本卡死 继续重新 识别
        return result["message"]
    return ""
# 验证码地址
img_path = "./yzm.jpg"
res = base64_api(uname='luckyboyxlg', pwd='17346570232', img=img_path, typeid=3)
print(res)
print('识别后的验证码为：', res)

# 用户名和密码
email = '793390457@qq.com'
pwd = "xlg17346570232"
# 表单数据
data = {
        "__VIEWSTATE": "1FClS0KnSNVXOhY/k04JfwQB/bmEmb09u9DuGPwln527V7RWbOfsk027f5NQuTFCY8MLC1ru7SkJ42sDWYxePaAzbrNUWfElMQ+8Qc32NkrtGEy6O2f6KXCGGgHdic1XUXkCfd8GHPlWhLajW3sykYCeCsU=",
        "__VIEWSTATEGENERATOR": "C93BE1AE",
        "from": "http://so.gushiwen.cn/user/collect.aspx",
        "email": email,
        "pwd": pwd,
        "code": res,
        "denglu": "登录"
}
# 请求头
headers = {
    'Content-Type':'application/x-www-form-urlencoded',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
    'Referer':'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
}
res = session.post(url, headers=headers, data=data)
print(res.content.decode())